package com.kakavr.shop.controller;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Maps;
import com.kakavr.auth.util.EncryptionUtils;
import com.kakavr.base.controller.BaseController;
import com.kakavr.base.exception.ValidateParamException;
import com.kakavr.result.MsgResult;
import com.kakavr.result.constant.MsgConstants;
import com.kakavr.shop.service.IShopAdminService;
import com.kakavr.util.SessionUtils;
import com.kakavr.util.ValidatorUtils;

/**
 * 店铺管理
 * @author gang.yang
 *
 */
@RestController
@RequestMapping("shopAdmin")
public class ShopAdminController extends BaseController {
	@Resource
	private IShopAdminService shopAdminService;
	
	/**
	 * 添加店铺
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public Map<String, Object> add(HttpServletRequest request){
		String code = request.getParameter("code");
		if(StringUtils.isBlank(code)){
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "店铺登录名");
		}
		String password = request.getParameter("password");
		if(StringUtils.isBlank(password)){
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "店铺登录密码");
		}
		if(!ValidatorUtils.isPass(password)){
			throw new ValidateParamException(MsgConstants.MSG_CODE_2004,
					MsgConstants.MSG_CONTENT_2004, "店铺登录密码");
		}
		String username = request.getParameter("userName");
		if(StringUtils.isBlank(username)){
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "店主用户名");
		}
		Map<String, Object> map = this.getParam(request);
		map.put("code", code);
		map.put("password", EncryptionUtils.md5(password));
		map.put("empPass", EncryptionUtils.DEFAULT_PASS);
		map.put("username", username);
		map.put("isMaster", 0);
		shopAdminService.addShopAndEmp(map);
		return MsgResult.okResult();
	}
	
	/**
	 * 修改店铺
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "update", method = RequestMethod.POST)
	public Map<String, Object> update(HttpServletRequest request){
		String id = request.getParameter("id");
		if(!ValidatorUtils.isNumber(id)){
			throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
					MsgConstants.MSG_CONTENT_1002, "店铺id");
		}
		String empId = request.getParameter("empId");
		if(!ValidatorUtils.isNumber(empId)){
			throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
					MsgConstants.MSG_CONTENT_1002, "店主id");
		}
		Map<String, Object> map = this.getParam(request);
		map.put("id", id);
		map.put("empId", empId);
		shopAdminService.updateShopAndEmp(map);
		return MsgResult.okResult();
	}
	
	private Map<String, Object> getParam(HttpServletRequest request){
		String name = request.getParameter("name");
		if(StringUtils.isBlank(name)){
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "店铺名称");
		}
		String area = request.getParameter("area");
		if(StringUtils.isBlank(area)){
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "所在地");
		}
		String areaName = request.getParameter("areaName");
		if(StringUtils.isBlank(areaName)){
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "所在地");
		}
		String address = request.getParameter("address");
		if(StringUtils.isBlank(address)){
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "详细地址");
		}
		String gatheringName = request.getParameter("gatheringName");
		if(StringUtils.isBlank(gatheringName)){
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "合作形式");
		}
		String gatheringType = request.getParameter("gatheringType");
		if(StringUtils.isBlank(gatheringType)){
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "合作形式");
		}
		String regionId = request.getParameter("regionId");
		if(!ValidatorUtils.isNumber(regionId)){
			throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
					MsgConstants.MSG_CONTENT_1002, "区域");
		}
		String typeId = request.getParameter("typeId");
		if(!ValidatorUtils.isNumber(typeId)){
			throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
					MsgConstants.MSG_CONTENT_1002, "店铺类型");
		}
		String deposit = request.getParameter("deposit");
		if(!ValidatorUtils.isMoney(deposit)){
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "押金");
		}
		String status = request.getParameter("status");
		if(StringUtils.isBlank(status)){
			status = "0";
		}
		//是否驻店0：否 1：是
		String isResident = request.getParameter("isResident");
		if(StringUtils.isBlank(isResident)){
			isResident = "0";
		}
		String mobile = request.getParameter("mobile");
		if(StringUtils.isBlank(mobile)){
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "联系方式");
		}
		String employee = request.getParameter("employee");
		if(StringUtils.isBlank(employee)){
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "店主姓名");
		}
		Map<String, Object> map = Maps.newHashMap();
		map.put("name", name);
		map.put("area", area);
		map.put("areaName", areaName);
		map.put("address", address);
		map.put("gatheringName", gatheringName);
		map.put("gatheringType", gatheringType);
		map.put("deposit", deposit);
		map.put("regionId", regionId);
		map.put("typeId", typeId);
		map.put("status", status);
		map.put("mobile", mobile);
		map.put("employee", employee);
		map.put("isResident", isResident);
		return map;
	}
	
	/**
	 * 店铺列表分页
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "selectList", method = RequestMethod.GET)
	public Map<String, Object> selectList(HttpServletRequest request){
		Map<String, Object> map = this.initPaginationParams(request);
		map.put("partnerId", SessionUtils.getPartnerId());
		return MsgResult.okResultData(shopAdminService.selectShopByPage(map));
	}
	/**
	 * 店铺列表不分页
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "selectShopList", method = RequestMethod.GET)
	public Map<String, Object> selectShopList(HttpServletRequest request){
		Map<String, Object> map = Maps.newHashMap();
		map.put("partnerId", SessionUtils.getPartnerId());
		return MsgResult.okResultData(shopAdminService.selectShopList(map));
	}
	
	/**
	 * 店铺详情
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "shopInfo", method = RequestMethod.GET)
	public Map<String, Object> selectShopInfo(HttpServletRequest request){
		String id = request.getParameter("id");
		if(!ValidatorUtils.isNumber(id)){
			throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
					MsgConstants.MSG_CONTENT_1002, "店铺id");
		}
		Map<String, Object> map = Maps.newHashMap();
		map.put("storeId", id);
		return MsgResult.okResultData(shopAdminService.getShopInfo(map));
	}
	
	/**
	 * 重置店铺密码
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "resetPass", method = RequestMethod.POST)
	public Map<String, Object> resetPass(HttpServletRequest request) {
		String id = request.getParameter("id");
		if (!ValidatorUtils.isNumber(id)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
					MsgConstants.MSG_CONTENT_1002, "店铺id");
		}
		Map<String, Object> map = Maps.newHashMap();
		map.put("id", id);
		map.put("password", EncryptionUtils.DEFAULT_PASS);
		shopAdminService.updateShop(map);
		return MsgResult.okResult();
	}
	
	/**
	 * 重置店主密码
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "resetEmpPass", method = RequestMethod.POST)
	public Map<String, Object> resetEmpPass(HttpServletRequest request){
		String empId = request.getParameter("empId");
		if(!ValidatorUtils.isNumber(empId)){
			throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
					MsgConstants.MSG_CONTENT_1002, "店主id");
		}
		Map<String, Object> map = Maps.newHashMap();
		map.put("empId", empId);
		map.put("empPass", EncryptionUtils.DEFAULT_PASS);
		shopAdminService.updateEmployee(map);
		return MsgResult.okResult();
	}
	
	/**
	 * 修改店铺帐号状态
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "updateStatus", method = RequestMethod.POST)
	public Map<String, Object> updateStaffStatus(HttpServletRequest request) {
		String id = request.getParameter("id");
		if (!ValidatorUtils.isNumber(id)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
					MsgConstants.MSG_CONTENT_1002, "id");
		}
		String status = request.getParameter("status");
		if (!ValidatorUtils.isNumber(status)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
					MsgConstants.MSG_CONTENT_1002, "账号状态");
		}
		Map<String, Object> map = Maps.newHashMap();
		map.put("id", id);
		map.put("status", status);
		shopAdminService.updateShop(map);
		return MsgResult.okResult();
	}

}
